'use strict'

yjx.directive('carousel',function(){
    return{
        restrict:'EA',
        link:function(scope,element,attr){
            scope.$watch(attr.carousel,function () {
                var slider=element.find('.homeslider-wrap').children(),
                    bullet=element.find('.homebullet').children(),
                    length=slider.length,
                    current=0,
                    time='',
                    loop=function(){
                        slider.eq(current).fadeOut(100)
                        slider.eq(current).removeClass('carousel-show');
                        bullet.eq(current).removeClass('homelibg');
                        if (current==(length-1)){
                            current=-1;
                        }
                        current+=1;
                        slider.eq(current).fadeIn(500);
                        slider.eq(current).addClass('carousel-show');
                        bullet.eq(current).addClass('homelibg');
                    };
                slider.eq(0).show();
                slider.eq(current).addClass('carousel-show');
                bullet.eq(0).addClass('homelibg');
                time=setInterval(loop,4000);
                function showPic(index){
                    slider.eq(index).fadeIn(500).siblings().fadeOut(100);
                    slider.removeClass('carousel-show');
                    slider.eq(index).addClass('carousel-show');
                    bullet.eq(index).addClass('homelibg').siblings().removeClass('homelibg');
                    clearInterval(time);
                    current=index;
                    time=setInterval(loop,4000);
                }
                var minRange = 30, //最小有效滑动范围
                    readyMoved = true,//判断每次滑动开始的标记变量
                    touchX;//最初touch X坐标
                bullet.on('click',function(){
                    var index=$(this).index();
                    showPic(index);
                });

                /*实现左右滑动效果*/
                slider.on('touchstart',function(e){
                    if(readyMoved){
                        //console.log(e);
                        touchX = e.originalEvent.touches[0].clientX;
                        readyMoved=false;
                    }
                });
                slider.on('touchmove',function(e){
                    //e.preventDefault();
                    if(!readyMoved){
                        var releaseAt = e.originalEvent.changedTouches[0].clientX
                        if(releaseAt + minRange < touchX) {
                            readyMoved = true;
                            if(current == 0 ){
                                current=length-1;

                            }else{
                                current--
                            }
                            showPic(current);
                            //console.log("向左滑动")
                        } else if (releaseAt - minRange > touchX) {
                            readyMoved = true;
                            if(current == length -1) {
                                current = 0;
                            }else{
                                current++
                            }
                            showPic(current);
                            //console.log("向右滑动")
                        }
                    }
                });
                slider.on('touchend',function(e){
                    if(!readyMoved){
                        var releaseAt = e.originalEvent.changedTouches[0].clientX
                        if(releaseAt + minRange < touchX) {
                            readyMoved = true;
                            if(current == 0 ){
                                current=length-1;

                            }else{
                                current--
                            }
                            showPic(current);
                            //console.log("向左滑动")
                        } else if (releaseAt - minRange > touchX) {
                            readyMoved = true;
                            if(current == length -1) {
                                current = 0;
                            }else{
                                current++
                            }
                            showPic(current);
                            //console.log("向右滑动")
                        }
                    }
                });
            })
        }
    };
});
